/**
 * 产品修改js
 */
var comValidform=null;
var returnStatus=false;
var activity={
	curPage : 1,
	pageSize : 10,
	dataCache : null,
	 
	 
	activityUE:null,
		
	startEvent:function(){
		$("#recordTab ul").idTabs();
		 
	 
		comValidform=$(".com-addForm").Validform({
			datatype:{
				"m0-2" : /^[0-9]+(.[0-9]{1,2})?$/
			},
			tiptype:2,
			tipSweep:true,
			beforeSubmit:function(curform){
				var url = 'mgr/activityCtrl/updateactivityInfo';
				var params = jsUtils.serializeJson($('.com-addForm'));
				var cb = function(data){
					alert(data.msg);
					//成功返回
					if(data.resultCode==200){
						//关闭窗口并刷新数据
						art.dialog.close();
						returnStatus=true;
					}
				};
				sendApiNoLoadSync(url, params, cb, 'POST');
				return false;
			}
		});

		$('#addData').on(
			'click',
			function() {
				var thisObj = $(this);
				var path = APP_ADDRESS
					+ 'mgr/activity/activityPlan/activityAdd.jsp';
				art.dialog.open(path, {
					title : '添加活动',
					width : 900,
					height : 500,
					ok : function() {
						var returnStatus = this.iframe.contentWindow
							.submitForm();
						if (returnStatus) {
							art.dialog.close();
							activity.setPageChange(activity.curPage);
							activity.search(activity.curPage);
						}
						return returnStatus;
					},
					cancel : true
				});
			});
	},
	
	// 设置分页
	setPageChange : function(delCurPage) {
		var url = 'mgr/activityCtrl/list';
		var name = $.trim($('#searchModForm input[name="name"]')
				.val());
		var params = {
			'curPage' : 1,
			'pageSize' : 99999,
			'name' : name
		};
		var cb = function(data) {
			if (data.resultCode == 200) {
				var totalCount = data.size;
				var pageTotal = (totalCount / activity.pageSize > 0) ? Math
						.ceil(totalCount / activity.pageSize) : 1;
				pageHelper.init(pageTotal, totalCount, activity.search,
						delCurPage); // 翻页事件
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	// 查询
	search : function(pageIdx) {
		var url = 'mgr/activityCtrl/list';
		if (typeof (pageIdx) != "undefined") {
			activity.curPage = pageIdx;
		}
		var name = $.trim($('#searchModForm input[name="name"]').val());
		var params = {
			'curPage' : activity.curPage,
			'pageSize' : activity.pageSize,
			'name' : name
		};
		var cb = function(data) {
			$('#tableList tbody').empty();
			var tr = '';
			if (data.resultCode == 200) {
				if (data.result != null && data.result.length > 0) {
					for (var i = 0; i < data.result.length; i++) {
						var singleObj = data.result[i];
						var orderCreatedOn = getSmpFormatDateByLong(singleObj.cts,
								true);
						localStorage.setItem(singleObj.id + "_activity_Key", JSON.stringify(singleObj));

						var channel = "";

						if(singleObj.channel != '' && singleObj.channel != null){
							if(singleObj.channel.indexOf("1") >= 0){
								channel += "- 华财通 -";
							}

							if(singleObj.channel.indexOf("2") >= 0){
								channel += "- 理财师 -";
							}

							if(singleObj.channel.indexOf("3") >= 0){
								channel += "- PC -";
							}
						}
						var startStatus = "";
						if(singleObj.startStatus != null){
							startStatus=jsUtils.getDictionaryTxtByType('actStatus', singleObj.startStatus);
						}

						var status = "";
						if(singleObj.status == 0){
							status = "未发布";
						} else if(singleObj.status == 1){
							status = "发布中";
						}else if (singleObj.status == 2){
							status = "已下架";
						}

						tr += '<tr><td><input type="hidden" name="id" value="'
								+ singleObj.id
								+ '"/>'
								+ singleObj.id
								+ '</td><td>'
								+ singleObj.name
								+ '</td><td>'
								+ singleObj.picAddress
								+ '</td><td>'
								+ singleObj.url
								+ '</td><td>'
								+ startStatus
								+ '</td><td>'
								+ status
								+ '</td><td>'
								+ channel
								+ '</td><td>'
								+ orderCreatedOn
								+ '</td><td>'
								+ '<a type="button" name="activity_96" class="btn btn-success btn-xs updateData" style="margin-left: 5px;">'
								+ '<i class="fa fa-edit"></i>修改</a>'
								+ '<a type="button" name="activity_91" class="btn btn-success btn-xs updateStatus" style="margin-left: 5px;">'
								+ '<i class="fa fa-edit"></i>编辑状态</a>'
								+ '</td></tr>';
					}
				} else {
					tr = '<tr><td colspan="7" style="text-align: center;color: red;">暂无数据</td></tr>';
				}
			}else {
				tr = '<tr><td colspan="7" style="text-align: center;color: red;">暂无数据</td></tr>';
			}
			$('#tableList tbody').html(tr);
			 
			jsUtils.controlFunctionBtn();
			activity.bindEvent();
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	bindEvent: function() {
		//查询
		$('#searchData').click(function(){
			activity.curPage=1;
			activity.setPageChange(null);
			activity.search();
		});
		$('.updateData').unbind();
		$('.updateData').on(
				'click',
				function() {
					var thisObj = $(this);
					 
					var id = thisObj.parent().parent().find('input[name="id"]')
							.eq(0).val();
					art.dialog.data('updId', id);
					 
					var path = APP_ADDRESS
							+ 'mgr/activity/activityPlan/activityUpdate.jsp';
					art.dialog.open(path, {
						title : '修改活动',
						width : 900,
						height : 400,
						ok : function() {
							var returnStatus = this.iframe.contentWindow
									.submitForm();
							if (returnStatus) {
								art.dialog.close();
								activity.setPageChange(activity.curPage);
								activity.search(activity.curPage);
							}
							return returnStatus;
						},
						cancel : true
					});
				});
		$('.updateStatus').unbind();
		$('.updateStatus').on(
				'click',
				function() {
					var thisObj = $(this);
					 
					var id = thisObj.parent().parent().find('input[name="id"]')
							.eq(0).val();
					art.dialog.data('updId', id);
					 
					var path = APP_ADDRESS
							+ 'mgr/activity/activityPlan/activityUpdateStatus.jsp';
					art.dialog.open(path, {
						title : '修改活动状态',
						width : 600,
						height : 300,
						ok : function() {
							var returnStatus = this.iframe.contentWindow
									.submitForm();
							if (returnStatus) {
								activity.setPageChange(activity.curPage);
								activity.search(activity.curPage);
								art.dialog.close();
							}
							return returnStatus;
						},
						cancel : true
					});
				});
		

		
	},
	showInfoById:function(){
		var updId=art.dialog.data('updId');  //修改id
		var url = 'mgr/activityCtrl/getAgrrementInfoById';
		var params ={
				'id':updId
		};
		var cb = function(data){
			if(data.resultCode==200){
				var ret=data.result;
				 
				if(ret.activityTxt!=null){
					activity.activityUE.ready(function() {
						activity.activityUE.setContent(ret.riskctrlTxt);
	                });
				}
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	init:function(){
		activity.setPageChange(null);
		activity.search();
		activity.startEvent();
		//activity.bindEvent();
	}
}
$(document).ready(function() {
	activity.init();
});
function submitForm(){
	comValidform.submitForm();
	return returnStatus;
}